package com.guyj.baseapp.demo.gridtitle;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.view.ViewGroup;

import com.guyj.baseapp.R;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class GridAdapter extends RecyclerView.Adapter<ViewHolder>{

    private static final int TITLE = 0;
    private static final int ITEM = 1;
    private List<List<String>> beans;
    private int titleSize;//Title的size
    private int tempInt;
    private List<Integer> itemSize;//Title内的item的size
    private Context con;
    public GridAdapter(Context con,List<List<String>> beans) {
        this.beans = beans;
        this.con=con;
        itemSize=new ArrayList<>();
        titleSize=beans.size();
        itemSize.add(0);
        tempInt=0;
        for (int i = 0; i < titleSize-1; i++) {
            tempInt+=beans.get(i).size()+1;
            itemSize.add(tempInt);
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==TITLE){
            View view=View.inflate(con, R.layout.layout_gridtitle,null);
            return new MViewHolderTitle(view);
        }else if(viewType==ITEM){
            View view=View.inflate(con,R.layout.layout_griditem,null);
            return new MViewHolderItem(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (holder instanceof MViewHolderTitle){

        }else if(holder instanceof MViewHolderItem){

        }
    }

    @Override
    public int getItemCount() {
        int itemCount=0;
        for (int i = 0; i < titleSize; i++) {
            itemCount+=beans.get(i).size();
        }
        return itemCount+titleSize;
    }

    @Override
    public int getItemViewType(int position) {

        if (itemSize.contains(position)){
            return TITLE;
        }else{
            return ITEM;
        }
    }

    class MViewHolderTitle extends ViewHolder{

        public MViewHolderTitle(View itemView) {
            super(itemView);
        }
    }
    class MViewHolderItem extends ViewHolder{

        public MViewHolderItem(View itemView) {
            super(itemView);
        }
    }
}
